package online;

import org.apache.zookeeper.*;

import java.io.IOException;

public class ShopServer {
    private static String connectString = "192.168.200.20:2181";
    private static int sessionTimeout = 60000;
    private ZooKeeper zk = null;

    // 创建到zk的客户端连接
    public void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent event) {
            }
        });
    }

    // 注册到集群
    public void register(String ShopName) throws Exception {
        // 一定是"EPHEMERAL_SEQUENTIAL短暂有序型"的节点，才能给shop编号，shop1，shop2...”
        String create = zk.create("/meituan/Shop", ShopName.getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println("【" + ShopName + "】 开始营业！ " + create);
    }

    // 业务功能
    public void business(String ShopName) throws Exception {
        System.out.println("【" + ShopName + "】 正在营业中 ...");
        System.in.read();
    }

    public static void main(String[] args) throws Exception {
        ShopServer shop = new ShopServer();
        // 1.连接zookeeper集群（和美团取得联系）
        shop.getConnect();
        // 2.将服务器节点注册（入住美团）
        shop.register(args[0]);
        // 3.业务逻辑处理（做生意）
        shop.business(args[0]);
    }
}
